package com.dongjiantong.user.manager;

import com.dongjiantong.common.utils.*;
import com.dongjiantong.user.mapper.LoginUserMapper;
import com.dongjiantong.user.mapper.MzUserMapper;
import com.dongjiantong.user.mapper.OrgUserAreaMapper;
import com.dongjiantong.user.model.LoginUser;
import com.dongjiantong.user.model.MzUser;
import com.dongjiantong.user.enums.UserEnum;
import com.dongjiantong.user.model.UserArea;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @author cao
 * @create 2017-11-24 上午10:47
 **/
@Slf4j
@Component
public class OrgAdminManager {

    @Resource
    private MzUserMapper mzUserMapper;
    @Resource
    private LoginUserMapper loginUserMapper;

    @Resource
    private OrgUserAreaMapper orgUserAreaMapper;

    public void createOrgAdmin(Long relationId,
                               Map<String,Object> area,
                               String areaName)
    {
        MzUser mzUser = new MzUser();
        LoginUser loginUser = new LoginUser();

        String realName=areaName+"管理员";

        String mobile = "2"+RandomUtil.generateNumberString(10);

        mzUser.setRealname(realName);
        mzUser.setMobile(mobile);
        mzUser.setUserType(UserEnum.USERTYPE_ORG.getCode());
        mzUser.setGender(UserEnum.Gender_UNKOWN.getCode());
        mzUser.setTitle("管理员");
        mzUser.setAvatar("");
        mzUser.setIdcard("");
        mzUser.setIsmaster(1);
        mzUser.setIsacert(1);
        mzUser.setIsbcert(1);
        mzUser.setProvince(area.get("province").toString());
        mzUser.setCity(area.get("city").toString());
        mzUser.setRegion(area.get("region").toString());
        mzUser.setStreet(area.get("street").toString());
        mzUser.setVillage("");
        mzUser.setProvinceId(IntegerUtil.toInt(area.get("provinceId").toString()));
        mzUser.setCityId(IntegerUtil.toInt(area.get("cityId").toString()));
        mzUser.setRegionId(IntegerUtil.toInt(area.get("regionId").toString()));
        mzUser.setStreetId(IntegerUtil.toInt(area.get("streetId").toString()));
        mzUser.setVillageId(0);
        mzUser.setRelationId(relationId);
        mzUser.setDeleteFlag(1);
        mzUser.setCreatedId(1L);
        mzUser.setUpdatedId(1L);
        mzUser.setCreatedAt(new Date());
        mzUser.setUpdatedAt(new Date());


        mzUserMapper.insert(mzUser);
        long userId = mzUser.getId();

        loginUser.setUserId(userId);
        loginUser.setUsername(mobile);
        loginUser.setPassword("mz2017");
        loginUser.setSalt("xyz");
        loginUser.setStatus(1);
        loginUser.setLogincount(0);
        loginUser.setErrorcount(0);
        loginUser.setUnlocktime(new Date());

        //密码加密
        String newPassword= MD5Util.encrypt(loginUser.getSalt() + loginUser.getPassword());
        loginUser.setPassword(newPassword);

        loginUserMapper.insert(loginUser);

        //设置区域
        UserArea userArea = new UserArea();
        userArea.setProvinceId(IntegerUtil.toInt(area.get("provinceId").toString()));
        userArea.setCityId(IntegerUtil.toInt(area.get("cityId").toString()));
        userArea.setRegionId(IntegerUtil.toInt(area.get("regionId").toString()));
        userArea.setStreetId(IntegerUtil.toInt(area.get("streetId").toString()));
        orgUserAreaMapper.insert(userArea);

    }


}
